<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\AdminController as Controller;
use Illuminate\Support\Facades\DB;
use Validator;
use Illuminate\Http\Request; 
use App\Model\{Admin,User,Config,Productcat,Product,Productattr,Order}; 
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redis;
use App\Rules\Money;
use App\Libraries\vendor\Kdapi;

/**
 * @menu_cat 订单
 * @icon asterisk
 * @sort 6000
 */
class OrderController extends Controller
{
	
    public function __invoke(){
        return $this->index(func_get_args());
    }

    /**
     * @menu 订单列表
     * @author wudi
     */
    public function index(){
    	$search = request()->input('_search');
    	$temp = [];
    	if($search){
    		$search = explode('|', urldecode($search));
    		foreach($search as $k=>&$v){
    			$v = explode('=', $v);
    			$temp[$v[0]] = $v[1];
    		}
    	}
        
        $search = $temp;
        $list = Order::order_list($search);

        return view('Admin.Order.index')->with([
            'list' => $list,
            '_search'=>$search,
            'order_pay_status'=>Config::get_config_bykey('order_pay_status'),
            'order_ship_status'=>Config::get_config_bykey('order_ship_status'),
        ]);
    }

    /**
     * 订单详情 
     * @name 订单详情
     */
    public function detail($id){
        
        $order = DB::table('order')->where('id',$id)->first();
        $product = DB::table('order_product')->where('order_id',$order->id)->first();
        $order_ship_status = COnfig::get_config_bykey('order_ship_status');
        $order_pay_status = COnfig::get_config_bykey('order_pay_status');
        return view('Admin.Order.orderdetail')->with([
            'order'=>$order,
            'product'=>$product,
            'order_pay_status'=>$order_pay_status,
            'order_ship_status'=>$order_ship_status,
            'order_ship_name'=>Config::get_config_bykey('order_ship_name'),
        ]);
    }

    public function ajax_update_detail(Request $request){
        if(request()->isMethod('post')){
            $api = new Kdapi();
            $param = $request->only(['id','ship_name','ship_no','province','city','area','address']);


            $validated = $request->validate([
                'province'=>'required',
                'ship_name' => 'required',
                'ship_no' => 'nullable',
                'city' => 'required',
                'area' => 'required',
                'address' => 'required',
            ]);
            $id = $param['id'];
            $order = DB::table('order')->where('id',$id)->first();

            $product = DB::table('order_product')->where('order_id',$order->id)->first();

            if($order->ship_status==1){
                return back();
            }

            $eorder = [];
            $eorder["ShipperCode"] = $param['ship_name'];
            $eorder["OrderCode"] = $order->order_no;
            $eorder["PayType"] = 1;
            $eorder["ExpType"] = 1;

            $sender = [];
            $sender["Name"] = "上海创鲲信息技术有限公司";
            $sender["Mobile"] = "17721134773";
            $sender["ProvinceName"] = "上海市";
            $sender["CityName"] = "上海市";
            $sender["ExpAreaName"] = "嘉定区";
            $sender["Address"] = "华江路348号申窑艺术中心A栋209室";

            $receiver = [];
            $receiver["Name"] = $order->real_name;
            $receiver["Mobile"] = $order->phone;
            $receiver["ProvinceName"] = $param['province'];
            $receiver["CityName"] = $param['city'];
            $receiver["ExpAreaName"] = $param['area'];
            $receiver["Address"] = $param['address'];
         
            $commodityOne = [];
            $commodityOne["GoodsName"] = $product->product_name;
            $commodity = [];
            $commodity[] = $commodityOne;

            $eorder["Sender"] = $sender;
            $eorder["Receiver"] = $receiver;
            $eorder["Commodity"] = $commodity;


            //调用电子面单
            $jsonParam = json_encode($eorder, JSON_UNESCAPED_UNICODE);
            $jsonResult = $api->submitEOrder($jsonParam);
            $result = json_decode($jsonResult,true);
            if($result['Success']==true){
                DB::table('order')->where('id',$param['id'])->update([
                    'ship_name'=>$param['ship_name'],
                    'ship_no'=>$result['Order']['LogisticCode'],
                    'ship_status'=>1,
                    'ship_time'=>time(),
                ]);

                DB::table('logistics_info')->insert([
                    'uid'=>$order->uid,
                    'order_id'=>$order->id,
                    'LogisticCode'=>$result['Order']['LogisticCode'],
                    'ShipperCode'=>$param['ship_name'],
                    'time'=>time()
                ]);

            }else{
                dd($jsonResult);
            }
        
            return back();
        }
    }
}